//
//  BPAnimationView.swift
//  Boomplay
//
//  Created by frank-du on 2019/7/16.
//  Copyright © 2019 全达晖. All rights reserved.
//

import UIKit
import Lottie

@objcMembers
public class BPAnimationView: UIView {
    var amView: AnimationView!
    var am: Animation!
    var amName: String!
    var backgroundBehavior: BPAnimationBackgoundBehavior {
        set {
            switch newValue {
            case BPAnimationBackgoundBehavior.stop:
                self.amView.backgroundBehavior = .stop
            case BPAnimationBackgoundBehavior.pause:
                self.amView.backgroundBehavior = .pause
            case BPAnimationBackgoundBehavior.pauseAndRestore:
                self.amView.backgroundBehavior = .pauseAndRestore
            case BPAnimationBackgoundBehavior.forceFinish:
                self.amView.backgroundBehavior = .forceFinish
            default: break
            }
        }
        
        get {
            switch self.amView.backgroundBehavior {
            case .stop:
                return BPAnimationBackgoundBehavior.stop
            case .pause:
                return BPAnimationBackgoundBehavior.pause
            case .pauseAndRestore:
                return BPAnimationBackgoundBehavior.pauseAndRestore
            case .forceFinish:
                return BPAnimationBackgoundBehavior.forceFinish
            }
        }
    }
    
    private func commonInit(animationName: String) {
        amName = animationName
        amView = AnimationView()
        am = Animation.named(amName, bundle: Bundle.main)
        amView.animation = am;
        self.addSubview(amView);
        
        self.backgroundBehavior = BPAnimationBackgoundBehavior.pauseAndRestore;
    }
    
    public init(animationName: String) {
        super.init(frame: CGRect.zero)
        self.commonInit(animationName: animationName)
    }
    
    public init(animationName: String, frame:CGRect) {
        super.init(frame: frame)
        self.commonInit(animationName: animationName)
    }
    
    @available(*, unavailable)
    override public init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()
        amView.frame = self.bounds
    }
    
    public func bp_play(fromProgress: CGFloat,
                        toProgress: CGFloat, loopMode: BPAnimationLoopMode) {
        var lpm = LottieLoopMode.playOnce
        
        switch loopMode {
        case BPAnimationLoopMode.playOnce:do {
            lpm = LottieLoopMode.playOnce
            break
        }
        case BPAnimationLoopMode.loop:do {
            lpm = LottieLoopMode.loop
            break
        }
        case BPAnimationLoopMode.autoReverse:
            lpm = LottieLoopMode.autoReverse
            break
        default:
            break
        }
        
        amView?.play(fromProgress: fromProgress, toProgress: toProgress, loopMode: lpm)
    }
    
    public func bp_play() {
        amView?.play()
    }
    
    public func bp_stop() {
        amView?.stop()
    }
    
    public func bp_pause() {
        amView?.pause()
        
    }
    
    public var isAnimationPlaying: Bool {
        return amView?.isAnimationPlaying ?? false
    }
    
//    @objc(playWithLoopMode:)
//    public func play(loopMode: LottieLoopMode) {
//
//    }
    
//    @objc(playWithLoopMode:)
//    public func play(loopMode: LottieLoopMode? = nil) {
//        amView.play(fromProgress: 0, toProgress: 1, loopMode: loopMode)
//    }
}
